/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package java_project;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.*;
import JavaSource.*;
import java.util.LinkedList;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author mac
 */
public class DishInfoGUI extends javax.swing.JFrame {

    /**
     * Creates new form DishInfoGUI
     */
    public DishInfoGUI() {
        initComponents();
        this.setLocationRelativeTo( null );
    }
    
    public DishInfoGUI(FoodOrderingSystem fos, ManagerGUI mf){
        this.fos = fos;
        this.mf = mf;                
        initComponents();  
        this.setLocationRelativeTo( null );
        fosList = fos.getIngredientList(); 
        for(int i=0; i < fosList.size(); i++) {
            ingredientName.addItem(fosList.get(i).getName());
        }                 
         jLabel1.setText("Add Dish");
         jButton1.setEnabled(true);
         ingredientName.setEnabled(true);
         quantity.setEnabled(true);
         addIngredient.setEnabled(true);
         removeIngredient.setEnabled(true);
    }
    
    public DishInfoGUI(FoodOrderingSystem fos, Dish dish, ManagerGUI mf){
        this.fos = fos;
        this.dish = dish; 
        this.mf = mf;
        initComponents(); 
        this.setLocationRelativeTo( null );
         fosList = fos.getIngredientList();
        for(int i=0; i < fosList.size(); i++) {
            ingredientName.addItem(fosList.get(i).getName());
        }        
            jLabel1.setText("Dish Detail");
            jButton3.setEnabled(true);
            dishNameInput.setText(dish.getName());
            priceInput.setText(" "+dish.readPrice());
            availableCheck.setSelected(dish.isAvailability(true));
            DefaultTableModel m = (DefaultTableModel)(jTable1.getModel());
            m.setRowCount(0);
            for(int i = 0; i < dish.getIngredient().size(); i++){
            Ingredient in = dish.getIngredient().get(i);
            Object ob[] = {in.getName(), in.getAmount()};
            m.addRow(ob);
            }        
            this.picture = dish.getPicture();
            try{
            jLabel3.setIcon(getImageIcon(picture));        
            }catch(Exception e ){
            
        }
    }
    


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();
        jFileChooser1 = new javax.swing.JFileChooser();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        dishNameInput = new javax.swing.JTextField();
        priceInput = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        availableCheck = new javax.swing.JCheckBox();
        jLabel6 = new javax.swing.JLabel();
        ingredientName = new javax.swing.JComboBox();
        quantity = new javax.swing.JComboBox();
        addIngredient = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        removeIngredient = new javax.swing.JButton();

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setType(java.awt.Window.Type.POPUP);

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel2.setText("Dish Name:");

        dishNameInput.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N

        priceInput.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N

        jLabel4.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel4.setText("Dish Price:");

        jLabel5.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel5.setText("Availability:");

        availableCheck.setText("Available");

        jLabel6.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel6.setText("Ingredient:");

        ingredientName.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        ingredientName.setEnabled(false);

        quantity.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        quantity.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7" }));
        quantity.setEnabled(false);
        quantity.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quantityActionPerformed(evt);
            }
        });

        addIngredient.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        addIngredient.setText(" Add Ingredient");
        addIngredient.setEnabled(false);
        addIngredient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addIngredientActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel7.setText("Photo:");

        jButton1.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jButton1.setText("Add");
        jButton1.setEnabled(false);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        jButton2.setText("select picture");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jButton3.setText("Update");
        jButton3.setEnabled(false);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Ingredient Name", "Ingredient Amout"
            }
        ));
        jScrollPane2.setViewportView(jTable1);

        removeIngredient.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        removeIngredient.setText(" Remove Ingredient");
        removeIngredient.setEnabled(false);
        removeIngredient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeIngredientActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(48, 48, 48)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(251, 251, 251)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jButton3)
                                    .addComponent(jButton1)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addGap(38, 38, 38)
                                .addComponent(jButton2)
                                .addGap(374, 374, 374)))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel6))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(55, 55, 55)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addComponent(priceInput, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(availableCheck, javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                                .addComponent(ingredientName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(quantity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(dishNameInput, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(47, 47, 47)
                                .addComponent(addIngredient)
                                .addGap(18, 18, 18)
                                .addComponent(removeIngredient)))
                        .addGap(0, 39, Short.MAX_VALUE))))
            .addGroup(layout.createSequentialGroup()
                .addGap(253, 253, 253)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(53, 53, 53)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(dishNameInput, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(priceInput, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(availableCheck))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(ingredientName, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(quantity, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(addIngredient, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(removeIngredient, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(60, Short.MAX_VALUE))
        );

        jButton3.getAccessibleContext().setAccessibleDescription("");

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private ImageIcon getImageIcon(String fileName){
        
        javax.swing.ImageIcon icon = new javax.swing.ImageIcon(fileName);
        Image img = icon.getImage() ;  
        Image newimg = img.getScaledInstance( 200, 200,  java.awt.Image.SCALE_SMOOTH ) ;
        return new javax.swing.ImageIcon(newimg);
    }
    
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
      int returnVal = jFileChooser1.showOpenDialog(this);
    if (returnVal == jFileChooser1.APPROVE_OPTION) {
        File file = jFileChooser1.getSelectedFile();
        String fileName = file.getPath();
        this.picture = fileName;
        //jLabel3.setIcon(new javax.swing.ImageIcon(fileName));        
        jLabel3.setIcon(getImageIcon(fileName));
    } else {
        System.out.println("File access cancelled by user.");
    }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void quantityActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_quantityActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_quantityActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        String name = dishNameInput.getText();
        double price = Double.parseDouble(priceInput.getText());
        boolean avai = availableCheck.isSelected();
        String p = this.picture;
        dish.updateDish(name, price, avai, p);
        mf.refreshTable(mf.getjTable4(),mf.getDishList1());
        JOptionPane.showMessageDialog(null, "Dish Information Updated Successfully!");
        this.dispose();
    }//GEN-LAST:event_jButton3ActionPerformed

    public void showAllDishIngredient(){        
        DefaultTableModel m = (DefaultTableModel)(jTable1.getModel());
        m.setRowCount(0);
        for(int i = 0; i < dishIngList.size(); i++){
            Ingredient in = dishIngList.get(i);
            Object ob[] = {in.getName(), in.getAmount()};
            m.addRow(ob);
        }        
    }
    
    private void addIngredientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addIngredientActionPerformed
        // TODO add your handling code here:
        
        int index = ingredientName.getSelectedIndex();
        Ingredient ing = fosList.get(index);
        int amount = Integer.parseInt(quantity.getSelectedItem().toString());
        Ingredient dishIngredient = new Ingredient(amount,ing);        
        dishIngList = specificDish.getIngredient();
        dishIngList.add(dishIngredient);
        showAllDishIngredient();     
    }//GEN-LAST:event_addIngredientActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        specificDish.setName(dishNameInput.getText());
        double d = Double.parseDouble(priceInput.getText());
        specificDish.setPrice(d);
        specificDish.setAvailability(availableCheck.isSelected());
        specificDish.setPicture(this.picture);
        fos.getDishList().add(specificDish);
        mf.getDishList1().add(specificDish);
        mf.refreshTable(mf.getjTable4(), mf.getDishList1());
        JOptionPane.showMessageDialog(null, "Dish added successfully");
        this.dispose();        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void removeIngredientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeIngredientActionPerformed
        // TODO add your handling code here:
        int index = jTable1.getSelectedRow();
        Ingredient i = dishIngList.get(index);
        dishIngList.remove(i);
        JOptionPane.showMessageDialog(null, "Ingredient deleted!"); 
        showAllDishIngredient();
    }//GEN-LAST:event_removeIngredientActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DishInfoGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DishInfoGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DishInfoGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DishInfoGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DishInfoGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addIngredient;
    private javax.swing.JCheckBox availableCheck;
    private javax.swing.JTextField dishNameInput;
    private javax.swing.JComboBox ingredientName;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JFileChooser jFileChooser1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField priceInput;
    private javax.swing.JComboBox quantity;
    private javax.swing.JButton removeIngredient;
    // End of variables declaration//GEN-END:variables
    private FoodOrderingSystem fos;
    private ManagerGUI mf;
    private Dish dish;
    private String picture;
    private LinkedList<Ingredient> fosList;
   private Dish specificDish = new Dish();
   private LinkedList<Ingredient> dishIngList;
   
}
